package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.dao.UecTitleDao;
import cn.ffcs.uec.staff.model.UecTitle;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecTitleDao")
@Transactional
public class UecTitleDaoImpl extends BaseDaoImpl implements UecTitleDao {

	@Override
	public PageInfo queryPageInfoByUecTitle(UecTitle uecTitle, int current, int pageSize) {
		if (null != uecTitle) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TITLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			// 判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecTitle.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}

			if (!StrUtil.isNullOrEmpty(uecTitle.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecTitle.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecTitle.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecTitle.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecTitle.class);
		}
		return null;
	}

	@Override
	public UecTitle findUecTitleById(UecTitle uecTitle) {
		if (null != uecTitle) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TITLE T WHERE T.TITLE_ID = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(uecTitle.getTitleId());
			List<UecTitle> uecTitles = this.jdbcFindList(sb.toString(), params, UecTitle.class);
			if (null != uecTitles && uecTitles.size() > 0) {
				return uecTitles.get(0);
			}
		}
		return uecTitle;
	}

	@Override
	public UecTitle findUecTitle(UecTitle uecTitle) {
		if (uecTitle != null) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TITLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			// 判断这个对象的状态
			if (!StrUtil.isNullOrEmpty(uecTitle.getStatusCd())) {
				sb.append(" AND T.STATUS_CD = ? ");
				params.add(uecTitle.getStatusCd());
			}
			// 判断这个对象是外包人员的数据还是合同制的数据
			if (!StrUtil.isNullOrEmpty(uecTitle.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			if (!StrUtil.isNullOrEmpty(uecTitle.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecTitle.getStaffId());
			}

			if (!StrUtil.isNullOrEmpty(uecTitle.getTitleId())) {
				sb.append(" AND T.TITLE_ID = ? ");
				params.add(uecTitle.getTitleId());
			}
			List<UecTitle> list = this.jdbcFindList(sb.toString(), params, UecTitle.class);

			if (list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfoByUecTitleOnApproval(UecTitle uecTitle, int current, int pageSize) {
		if (null != uecTitle) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TITLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);

			// 判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecTitle.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}

			if (!StrUtil.isNullOrEmpty(uecTitle.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecTitle.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecTitle.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecTitle.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecTitle.class);
		}
		return null;
	}

	@Override
	public UecTitle findUecTitleByUecOutStaffInfo(UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_TITLE T WHERE 1 = 1 AND T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ?");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			List<UecTitle> list = this.jdbcFindList(sb.toString(), params,
					UecTitle.class);
			if (list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public List<UecTitle> getUecTitleListByPkpsndoc(StaffAndOutStaff uecOutStaffInfo) {
		if (uecOutStaffInfo != null) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_TITLE T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
				sb.append(" AND T.STATUS_CD = ? ");
				params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
				if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkPsndoc())) {
					sb.append(" AND T.PK_PSNDOC = ? ");
					params.add(uecOutStaffInfo.getPkPsndoc());
				}
				sb.append(" ORDER BY T.GLBDEF4 ");
			List<UecTitle> list = this.jdbcFindList(sb.toString(), params, UecTitle.class);

			if (list.size() > 0) {
				return list;
			}
		}
		return null;
	}
}
